//
//  SlideArriveView.m
//  one_buy_guizhou_ios
//
//  Created by xu yao on 2021/2/2.
//  Copyright © 2021 杨帅. All rights reserved.
//

#import "SlideArriveView.h"
#import "UIView+Extension.h"
#import "NaviDeliveryGoodsView.h"
#import "GAppConfig.h"

@interface SlideArriveView ()

@property (nonatomic, strong) UIView *rightView;
@property (nonatomic, strong) UIImageView *slideIView;
@property (nonatomic, strong) UIImageView *arrowIView1;
@property (nonatomic, strong) UIImageView *arrowIView2;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, assign) CGFloat beginPointX;

@end

@implementation SlideArriveView

+ (instancetype)slideArriveViewWithFrame:(CGRect)frame withOrderId:(NSString *)orderId withWaybillNo:(NSString *)waybillNo{
    SlideArriveView *arriveView = [[SlideArriveView alloc] initWithFrame:frame];
    arriveView.orderId = orderId;
    arriveView.waybillNo = waybillNo;
    return arriveView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:38.0/255.0 blue:49.0/255.0 alpha:1];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.arrowIView1];
    [self addSubview:self.arrowIView2];
    [self addSubview:self.titleLabel];
    [self addSubview:self.rightView];
    [self addSubview:self.slideIView];
    
    self.rightView.size = CGSizeMake(5, self.height);
    self.rightView.origin = CGPointMake(0, 0);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches  anyObject] locationInView:self];
    CGPoint point1 = [self.slideIView.layer convertPoint:point fromLayer:self.layer]; //get layer using containsPoint:
    if([self.slideIView.layer containsPoint:point1]){
        self.beginPointX = point.x;
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches  anyObject] locationInView:self];
    if(self.beginPointX > 0){
        CGFloat rightViewW = point.x - self.beginPointX;
        if (rightViewW < 0) {
            rightViewW = 0;
        } else if (rightViewW > self.width - self.slideIView.width + 15) {
            rightViewW = self.width - self.slideIView.width + 15;
            //成功
        }
        self.rightView.width = rightViewW;
        [self layoutSubviews];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.rightView.width >= self.width - self.slideIView.width + 15) {
        // 成功
        [self showDelivery];
    } else {
        self.rightView.width = 5;
        [self layoutSubviews];
    }
    self.beginPointX = 0;
}

- (void)showDelivery {
    if (self.waybillStatus == 3) {
        if (self.checkCodeSuccess) {
            self.checkCodeSuccess(YES);
        }
        return;
    }
    //判断一下是否需要打卡
    __weak typeof(self) weakSelf = self;
    [NetworkTool GetHttpDataWithUrlStr:getValidationStatus Dic:@{@"orderId":self.orderId, @"waybillNo":self.waybillNo} SuccessBlock:^(id  _Nonnull responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            switch ([responseObject[@"data"] intValue]) {
                case 1:
                {
                    [NaviDeliveryGoodsView deliveryGoodsView:self.waybillNo orderId:self.orderId superView:self.superview.superview onClose:^(BOOL isSuccess) {
                        if (weakSelf.checkCodeSuccess) {
                            weakSelf.checkCodeSuccess(isSuccess);
                        }
                    }];
                }
                    break;
                case 2: {
                    if (self.checkCodeSuccess) {
                        self.checkCodeSuccess(YES);
                    }
                }
                    break;
                case 3:
                {
                    [[HCToast shareInstance] showErrorIconToast:@"装车信息不符，请于发货人确认！"];
                }
                    break;
                default:
                    break;
            }
        } 
    } FailureBlock:^(id  _Nonnull error) {
        
    }];
    
    
}

- (void)setWaybillStatus:(int)waybillStatus {
    _waybillStatus = waybillStatus;
    self.titleLabel.text = waybillStatus == 2 ? @"右滑抵达取货地" : @"右滑抵达送货地";
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.slideIView.size = CGSizeMake(self.slideIView.width, self.height);
    self.slideIView.origin = CGPointMake(self.rightView.right - 5, 0);
    
    [self.arrowIView1 sizeToFit];
    self.arrowIView1.centerY = self.height * 0.5;
    self.arrowIView1.left = 88;
    
    [self.arrowIView2 sizeToFit];
    self.arrowIView2.centerY = self.height * 0.5;
    self.arrowIView2.left = 96;
    
    [self.titleLabel sizeToFit];
    self.titleLabel.centerY = self.arrowIView2.centerY;
    self.titleLabel.left = self.arrowIView2.right + 8;
}

#pragma mark 懒加载
- (UIImageView *)slideIView {
    if (!_slideIView) {
        _slideIView = [[UIImageView alloc] init];
//        _slideIView.backgroundColor = [UIColor colorWithRed:1 green:71.0/255.0 blue:0 alpha:1];
        _slideIView.image = [UIImage imageNamed:@"ic_slide"];
        _slideIView.size = CGSizeMake(80, 40);
    }
    return _slideIView;
}

- (UIImageView *)arrowIView1{
    if (!_arrowIView1) {
        _arrowIView1 = [[UIImageView alloc] init];
        _arrowIView1.image = [UIImage imageNamed:@"icon／路线备份 4"];
    }
    return _arrowIView1;
}

- (UIImageView *)arrowIView2{
    if (!_arrowIView2) {
        _arrowIView2 = [[UIImageView alloc] init];
        _arrowIView2.image = [UIImage imageNamed:@"icon／路线备份 4"];
    }
    return _arrowIView2;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor colorWithWhite:1 alpha:0.65];
        _titleLabel.text = @"右滑抵达取货地";
        _titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel;
}

-(UIView *)rightView {
    if (!_rightView) {
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = [UIColor colorWithRed:1 green:71.0/255.0 blue:0 alpha:1];
    }
    return _rightView;
}


@end
